﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class AddCompany : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated == false)
        {
            Response.Redirect("login.aspx");
        }

        if (Request.IsAuthenticated)
        {
            string n = "";
            SqlCommand sCmd = new SqlCommand("SELECT name FROM USERS WHERE id=@id", conn);
            sCmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = User.Identity.Name;
            conn.Open();
            SqlDataReader dr = sCmd.ExecuteReader();
            while (dr.Read())
            {
                string name = Convert.ToString(dr[0].ToString());
                n = name;
            }
            dr.Close();
            conn.Close();

            WelcomeBackMessage.Text = "Welcome back, " + n + "!";

            AuthenticatedHeaderPanel.Visible = true;
            AuthenticatedMessagePanel.Visible = true;
            AuthenticatedMenuPanel.Visible = true;
            if (!IsPostBack)
            {
                SqlCommand SelCmd = new SqlCommand("SELECT name, secondname FROM USERS WHERE id = @id",conn);
                SelCmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = User.Identity.Name;
                conn.Open();
                SqlDataReader dr1 = SelCmd.ExecuteReader();
                while (dr1.Read())
                {
                    lbl_user.Text = Convert.ToString(dr1[0] + " " + dr1[1]);
                }
                dr1.Close();
                conn.Close();
            }
        }
        else
        {
            AuthenticatedHeaderPanel.Visible = false;
            AuthenticatedMessagePanel.Visible = false;
            AuthenticatedMenuPanel.Visible = false;
        }
    }

    protected void bt_add_Click(object sender, EventArgs e)
    {
        string id_user = User.Identity.Name;
        string main_company = check_main_comp.Checked.ToString();
        string position = tb_position.Text;
        string comp_name = tb_comp_name.Text;
        string comp_reg_nr = tb_comp_reg_nr.Text;
        string comp_vat = cb_comp_vat.Checked.ToString();
        string client = "false";

        _User_company.Insert_Company(comp_name, comp_reg_nr, comp_vat, id_user, main_company, position, client);

        SqlCommand SelCmd_MAX = new SqlCommand("SELECT MAX(id) FROM company", conn);
        conn.Open();
        string company_id = Convert.ToString(SelCmd_MAX.ExecuteScalar());
        conn.Close();
        Session["Company_ID"] = company_id;
        Response.Redirect("ViewCompany.aspx");
    }

    protected void bt_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("MyCompanies.aspx");
    }
}